package com.cloudcc.mobile.view.activity;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.TextView;
import butterknife.Bind;
import butterknife.OnClick;

import com.cloudcc.cloudframe.bus.EventEngine;
import com.cloudcc.cloudframe.model.MenuTabModel;
import com.cloudcc.mobile.R;
import com.cloudcc.mobile.adapter.MenuFilterListAdapter;
import com.cloudcc.mobile.event.refresh.RefreshMainMenuEvent;
import com.cloudcc.mobile.manager.CacheManager;
import com.cloudcc.mobile.view.base.BaseActivity;

public class MenuFilterActivity extends BaseActivity {

	@Bind(R.id.listview)
	ListView mSortListView;

	@Bind(R.id.header_left)
	TextView header_left;

	@Bind(R.id.header_right)
	TextView header_right;

	private List<MenuTabModel> mMenuListData;

	private MenuFilterListAdapter sortAdapter;

	@Override
	public void init(Bundle savedInstanceState) {
		super.init(savedInstanceState);
		mMenuListData = (List<MenuTabModel>) getIntent().getSerializableExtra(
				"sort.filter");
		fillUI();
	}

	private void fillUI() {
		sortAdapter = new MenuFilterListAdapter(mContext);
		sortAdapter.changeData(mMenuListData);
		mSortListView.setAdapter(sortAdapter);

		List<MenuTabModel> ingoreMenus = CacheManager.getInstance()
				.getIgnoreMenu();
		if (ingoreMenus == null) {
			ingoreMenus = new ArrayList<MenuTabModel>();
		}
		List<MenuTabModel> choiceMenus = new ArrayList<MenuTabModel>();
		choiceMenus.addAll(mMenuListData);
		Collections.copy(choiceMenus, mMenuListData);
		choiceMenus.removeAll(ingoreMenus);
		sortAdapter.changeChoices(choiceMenus);
	}

	@Override
	public int getLayoutId() {
		return R.layout.menu_filter_layout;
	}

	@OnClick(R.id.header_left)
	public void onClickCancel() {
		this.finish();
	}

	@OnClick(R.id.container)
	public void onClickALL() {
		this.finish();
	}

	@OnClick(R.id.header_right)
	public void onClickDone() {
		// #1 保存没有选中的
		List<MenuTabModel> choices = sortAdapter.getChoices();
		mMenuListData.removeAll(choices);
		CacheManager.getInstance().cacheIgnoreMenu(mMenuListData);
		// #2 设置回退
		EventEngine.post(new RefreshMainMenuEvent());
		this.finish();
	}

}
